﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace brand.cyt369.com.dal.DbContexts
{
    //public partial class CYT369BrandContext : Microsoft.EntityFrameworkCore.DbContext
    public partial class CYT369BrandContext : Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityDbContext<brand.cyt369.com.models.Identity.SysUser,
                                                                                                        brand.cyt369.com.models.Identity.UserType,
                                                                                                        int,
                                                                                                        brand.cyt369.com.models.Identity.UserClaim,
                                                                                                        brand.cyt369.com.models.Identity.UserRole,
                                                                                                        brand.cyt369.com.models.Identity.UserLogin,
                                                                                                        brand.cyt369.com.models.Identity.RoleClaim,
                                                                                                        brand.cyt369.com.models.Identity.UserToken>
    {
        private brand.cyt369.com.utility.Connection m_Connection;
        public CYT369BrandContext()
        {
            m_Connection = brand.cyt369.com.utility.SystemSettingService.Instance.BrandDB;
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (m_Connection != null)
            {
                if (m_Connection.DBType == brand.cyt369.com.utility.ConstStringClass.SQLSERVER2008 ||
                    m_Connection.DBType == brand.cyt369.com.utility.ConstStringClass.SQLSERVER2005)
                {
                    optionsBuilder.UseSqlServer(m_Connection.ConnectionString).ReplaceService<Microsoft.EntityFrameworkCore.Query.IQueryTranslationPostprocessorFactory, Microsoft.EntityFrameworkCore.SqlServer.Query.Internal.SqlServer2008QueryTranslationPostprocessorFactory>();
                }
                else
                {
                    optionsBuilder.UseSqlServer(m_Connection.ConnectionString);
                }
            }
            base.OnConfiguring(optionsBuilder);
        }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            string schema = null;
            if (m_Connection != null)
            {
                schema = m_Connection.DBSchema;
            }

            DocModelCreating(builder, schema);
        }

        private void DocModelCreating(ModelBuilder modelBuilder, string schema)
        {



            modelBuilder.Entity<brand.cyt369.com.models.Brand>(entity =>
            {
                entity.HasKey(c => c.Id);
                               
            });

            modelBuilder.Entity<brand.cyt369.com.models.BrandExtension>(entity =>
            {
                entity.HasKey(c => c.Id);
                // 1对1
                entity.HasOne(c => c.Brand).WithOne(c => c.BrandExtension).HasForeignKey<brand.cyt369.com.models.BrandExtension>(c => c.BrandID);    
            });

            modelBuilder.Entity<brand.cyt369.com.models.BrandImage>(entity =>
            {
                entity.HasKey(c => c.Id);
                // 1对多 品牌-品牌图片
                entity.HasOne(c => c.Brand).WithMany(c => c.BrandImages).HasForeignKey(c => c.BrandID);
            });

            modelBuilder.Entity<brand.cyt369.com.models.Carousel>(entity =>
            {
                entity.HasKey(c => c.Id);
                // 1对多 品牌-品牌轮播图
                entity.HasOne(c => c.Brand).WithMany(c => c.Carousels).HasForeignKey(c => c.BrandID);
            });

            modelBuilder.Entity<brand.cyt369.com.models.Category>(entity =>
            {
                entity.HasKey(c => c.Id);
                //一对多 品牌-类别
                entity.HasMany(c=>c.Brands).WithOne(c=>c.Category).HasForeignKey(c => c.CategoryID);
            });

            modelBuilder.Entity<brand.cyt369.com.models.HotBrand>(entity =>
            {
                entity.HasKey(c => c.Id);
                entity.HasOne(c => c.Brand).WithMany(c => c.HotBrands).HasForeignKey(c => c.BrandID);
            });

            modelBuilder.Entity<brand.cyt369.com.models.Inquiry>(entity =>
            {
                entity.HasKey(c => c.Id);
                entity.HasOne(c => c.Brand).WithMany(c => c.Inquiries).HasForeignKey(c => c.BrandID);
            });

        }


    }
}
